package com.example.study.component;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;

@Component
public class MyServletRequestListener implements ServletRequestListener {

    private final static Logger logger = LoggerFactory.getLogger(MyServletRequestListener.class);

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        logger.info("request end");
        HttpServletRequest httpServletRequest = (HttpServletRequest) sre.getServletRequest();
        logger.info("保存的name为:{}",httpServletRequest.getAttribute("name"));
    }

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        HttpServletRequest httpServletRequest = (HttpServletRequest) sre.getServletRequest();
        logger.info("session的id为：",httpServletRequest.getRequestedSessionId());
        logger.info("request的url为:",httpServletRequest.getRequestURL());
        httpServletRequest.setAttribute("name","xll");
    }
}
